﻿using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using DifferenceEngine;
using TextLine = DifferenceEngine.TextLine;

namespace IISAutoDeployment
{
    /// <summary>
    /// Interaction logic for DiffResultsWindow.xaml
    /// </summary>
    public partial class DiffResultsWindow : Window
    {
        #region Observables
        //public ObservableCollection<DiffView> DiffViews { get; set; }
        //private ObservableCollection<DiffView> _backupViews = new ObservableCollection<DiffView>();
        //private ObservableCollection<DiffView> _deployViews = new ObservableCollection<DiffView>();

        //public ObservableCollection<DiffView> BackupViews
        //{
        //    get { return _backupViews; }
        //    set { _backupViews = value; }
        //}

        //public ObservableCollection<DiffView> DeployViews
        //{
        //    get { return _deployViews; }
        //    set { _deployViews = value; }
        //}
        #endregion

        public DiffResultsWindow(DiffListTextFile backupSource, DiffListTextFile deploySource, ArrayList diffLines, double seconds)
		{
			InitializeComponent();
            //_backupViews = new DiffViews();
            //_deployViews = new DiffViews();

            Title = string.Format("Results: {0} secs.", seconds.ToString("#0.00"));
            var cnt = 1;
            var changeCount = 0;
            foreach (DiffResultSpan drs in diffLines)
			{
			    int i;
			    ListViewItem backupLineNum;
                ListViewItem backupText;
			    ListViewItem deployLineNum;
			    ListViewItem deployText;

			    switch (drs.Status)
				{
					case DiffResultSpanStatus.DeleteSource:
						for (i = 0; i < drs.Length; i++)
						{
							backupLineNum = new ListViewItem
							{
							    Content = cnt.ToString("00000"), 
                                Background = new SolidColorBrush(Colors.Red)
							};
                            backupText = new ListViewItem
                            {
                                Content = ((TextLine)backupSource.GetByIndex(drs.SourceIndex + i)).Line,
                                Background = new SolidColorBrush(Colors.Red)
                            };
                            deployLineNum = new ListViewItem
                            {
                                Content = cnt.ToString("00000"), 
                                Background = new SolidColorBrush(Colors.LightGray)
                            };
                            deployText = new ListViewItem
                            {
                                Content = "",
                                Background = new SolidColorBrush(Colors.Red)
                            };

                            //BackupLineNumView.Items.Add(backupLineNum);
                            BackupTextLineView.Items.Add(backupText);
                            //DeployLineNumView.Items.Add(deployLineNum);
                            DeployTextLineView.Items.Add(deployText);

                            //BackupViews.Add(new DiffView(backupLineNum, backupText));
                            //DeployViews.Add(new DiffView(deployLineNum, deployText));
							cnt++;
						    changeCount++;
						}
						
						break;
					case DiffResultSpanStatus.NoChange:
						for (i = 0; i < drs.Length; i++)
						{
							backupLineNum = new ListViewItem
                            {
                                Content = cnt.ToString("00000"),
                                Background = new SolidColorBrush(Colors.White)
                            };
                            backupText = new ListViewItem
                            {
                                Content = ((TextLine)backupSource.GetByIndex(drs.SourceIndex + i)).Line,
                                Background = new SolidColorBrush(Colors.White)
                            };
                            deployLineNum = new ListViewItem
                            {
                                Content = cnt.ToString("00000"),
                                Background = new SolidColorBrush(Colors.White)
                            };
                            deployText = new ListViewItem
                            {
                                Content = ((TextLine)deploySource.GetByIndex(drs.DestIndex + i)).Line,
                                Background = new SolidColorBrush(Colors.White)
                            };

                            //BackupViews.Add(new DiffView(backupLineNum, backupText));
                            //DeployViews.Add(new DiffView(deployLineNum, deployText));

                            //BackupLineNumView.Items.Add(backupLineNum);
                            BackupTextLineView.Items.Add(backupText);
                            //DeployLineNumView.Items.Add(deployLineNum);
                            DeployTextLineView.Items.Add(deployText);
                            cnt++;
						}
						
						break;
					case DiffResultSpanStatus.AddDestination:
						for (i = 0; i < drs.Length; i++)
						{
							backupLineNum = new ListViewItem
                            {
                                Content = cnt.ToString("00000"),
                                Background = new SolidColorBrush(Colors.LightGray)
                            };
                            backupText = new ListViewItem
                            {
                                Content = "",
                                Background = new SolidColorBrush(Colors.LightGray)
                            };
                            deployLineNum = new ListViewItem
                            {
                                Content = cnt.ToString("00000"),
                                Background = new SolidColorBrush(Colors.LightGreen)
                            };
                            deployText = new ListViewItem
                            {
                                Content = ((TextLine)deploySource.GetByIndex(drs.DestIndex + i)).Line,
                                Background = new SolidColorBrush(Colors.LightGreen)
                            };

                            //BackupViews.Add(new DiffView(backupLineNum, backupText));
                            //DeployViews.Add(new DiffView(deployLineNum, deployText));

                            //BackupLineNumView.Items.Add(backupLineNum);
                            BackupTextLineView.Items.Add(backupText);
                            //DeployLineNumView.Items.Add(deployLineNum);
                            DeployTextLineView.Items.Add(deployText);
                            cnt++;
						    changeCount++;
						}
						
						break;
					case DiffResultSpanStatus.Replace:
						for (i = 0; i < drs.Length; i++)
						{
							backupLineNum = new ListViewItem
                            {
                                Content = cnt.ToString("00000"),
                                Background = new SolidColorBrush(Colors.Red)
                            };
                            backupText = new ListViewItem
                            {
                                Content = ((TextLine)backupSource.GetByIndex(drs.SourceIndex + i)).Line,
                                Background = new SolidColorBrush(Colors.Red)
                            };
                            deployLineNum = new ListViewItem
                            {
                                Content = cnt.ToString("00000"),
                                Background = new SolidColorBrush(Colors.LightGreen)
                            };
                            deployText = new ListViewItem
                            {
                                Content = ((TextLine)deploySource.GetByIndex(drs.DestIndex + i)).Line,
                                Background = new SolidColorBrush(Colors.LightGreen)
                            };

                            //BackupViews.Add(new DiffView(backupLineNum, backupText));
                            //DeployViews.Add(new DiffView(deployLineNum, deployText));

                            //BackupLineNumView.Items.Add(backupLineNum);
                            BackupTextLineView.Items.Add(backupText);
                            //DeployLineNumView.Items.Add(deployLineNum);
                            DeployTextLineView.Items.Add(deployText);
                            cnt++;
						    changeCount++;
						}
						
						break;
				}
			}
            MessageBox.Show("There are " + changeCount + " changes between the backup and deployment files",
                                    "Summary of diff results", MessageBoxButton.OK, MessageBoxImage.Information);
		}

        #region Event Handlers
        private void BackupView2_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (BackupTextLineView.SelectedItems.Count <= 0) return;
            ((ListViewItem)DeployTextLineView.Items.GetItemAt(BackupTextLineView.SelectedIndex)).IsSelected = true;
            ((ListViewItem)DeployTextLineView.Items.GetItemAt(BackupTextLineView.SelectedIndex)).Focus();
        }

        //private void BackupView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (BackupLineNumView.SelectedItems.Count <= 0) return;
        //    ((ListViewItem)BackupTextLineView.Items.GetItemAt(BackupLineNumView.SelectedIndex)).IsSelected = true;
        //    ((ListViewItem)BackupTextLineView.Items.GetItemAt(BackupLineNumView.SelectedIndex)).Focus();
        //}
        private void DeployView2_OnSelectionChanged(object sender, RoutedEventArgs e)
        {
            if (DeployTextLineView.SelectedItems.Count <= 0) return;
            ((ListViewItem)BackupTextLineView.Items.GetItemAt(DeployTextLineView.SelectedIndex)).IsSelected = true;
            ((ListViewItem)BackupTextLineView.Items.GetItemAt(DeployTextLineView.SelectedIndex)).Focus();
        }
        #endregion
    }
}
